package com.eric.order.controller;

import com.eric.api.dto.AccountDto;
import com.eric.api.dto.ProductDto;
import com.eric.api.feign.AccountFeign;
import com.eric.api.feign.ProductFeign;
import com.eric.order.entity.Order;
import com.eric.order.service.OrderService;
import entity.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author:Eric
 * @date:2021/7/7
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    AccountFeign accountFeign;

    @Autowired
    ProductFeign productFeign;

    @Autowired
    OrderService orderService;

    @PostMapping("/feign/insertAccount")
    public CommonResult insertAccount(@RequestBody AccountDto accountDto) {
        return accountFeign.insert(accountDto);
    }

    @PutMapping("/feign/updateAccount")
    public CommonResult updateAccount(@RequestBody AccountDto accountDto) {
        return accountFeign.update(accountDto);
    }

    @DeleteMapping("/feign/deleteAccount/{id}")
    public CommonResult deleteAccount(@PathVariable(value = "id") Long id) {
        return accountFeign.delete(id);
    }

    @GetMapping("/feign/getOneAccount/{id}")
    public CommonResult getOneAccount(@PathVariable(value = "id") Long id) {
        return accountFeign.getOne(id);
    }

    @PostMapping("/feign/insertProduct")
    public CommonResult insert(ProductDto product) {
        productFeign.insert(product);
        return CommonResult.success("success");
    }

    @PutMapping("/feign/updateProduct")
    public CommonResult update(ProductDto product) {
        return productFeign.update(product);
    }

    @DeleteMapping("/feign/deleteProduct/{id}")
    public CommonResult delete(@PathVariable(value = "id") Long id) {
        return productFeign.delete(id);
    }

    @GetMapping("/feign/getOneProduct/{id}")
    public CommonResult getOne(@PathVariable(value = "id") Long id) {
        return productFeign.getOne(id);
    }

    /**
     * 创建订单
     * @param order
     * @return
     */
    @PostMapping
    public CommonResult create(@RequestBody Order order) {
        orderService.create(order);
        return CommonResult.success("success");
    }
}
